package com.lyeducation.controllers.dto.system;

import com.lyeducation.entities.system.SysMenuEntity;
import com.lyeducation.entities.system.SysRoleEntity;
import lombok.Getter;

import java.util.*;

@Getter
public class ShowInsertUpdateSysMenuDto extends SysMenuDto {
    private List<SysRoleDto> roleList;

    public ShowInsertUpdateSysMenuDto() {
        super();
    }
//
//    public ShowInsertUpdateSysMenuDto(
//            String id,
//            String parentId,
//            TreeSet<SysMenuDto> childMenus,
//            List<SysRoleDto> roleList,
//            String name,
//            int status,
//            String url,
//            int level,
//            int sort) {
//        this.setId(id);
//        this.setParentId(parentId);
//        this.setChildMenus(childMenus);
//        this.roleList = roleList;
//        this.setName(name);
//        this.setStatus(status);
//        this.setUrl(url);
//        this.setLevel(level);
//        this.setSort(sort);
//    }

    public ShowInsertUpdateSysMenuDto(String id, String parentId, TreeSet<ShowInsertUpdateSysMenuDto> childMenus, List<SysRoleDto> roleList, String name, int status, String url, int level, int sort) {
        super(id, parentId, childMenus, name, status, url, level, sort);
        this.roleList = roleList;
    }

    public static ShowInsertUpdateSysMenuDto createSysMenuDto(SysMenuEntity sysMenuEntity) {

        TreeSet<SysMenuEntity> childMenusSet = sysMenuEntity.getChildren();
        TreeSet<ShowInsertUpdateSysMenuDto> childenMenuDtoSet = new TreeSet<>(new Comparator<ShowInsertUpdateSysMenuDto>() {
            @Override
            public int compare(ShowInsertUpdateSysMenuDto o1, ShowInsertUpdateSysMenuDto o2) {
                if (o1.getId().equals(o2.getId())) {
                    return 0;
                }
                if (o1.getSort() - o2.getSort() == 0) {
                    return 1;
                }
                return o1.getSort() - o2.getSort();
            }
        });
        if (childMenusSet != null && childMenusSet.size() != 0) {

            Iterator<SysMenuEntity> childMenusSetIterator = childMenusSet.iterator();
            while (childMenusSetIterator.hasNext()) {
                SysMenuEntity childMenu = childMenusSetIterator.next();
                childenMenuDtoSet.add(createSysMenuDto(childMenu));
            }
        }
        List<SysRoleDto> sysRoleDtoList = new ArrayList<SysRoleDto>();
        if (sysMenuEntity.getSysRoleList() != null && sysMenuEntity.getSysRoleList().size() > 0) {
            for (SysRoleEntity sysRoleEntity : sysMenuEntity.getSysRoleList()) {
                sysRoleDtoList.add(SysRoleDto.createSysRoleDtoBySysRoleEntity(sysRoleEntity));
            }
        }
        ShowInsertUpdateSysMenuDto sysMenuDto =
                new ShowInsertUpdateSysMenuDto(
                        sysMenuEntity.getId(),
                        sysMenuEntity.getParentId(),
                        childenMenuDtoSet,
                        sysRoleDtoList,
                        sysMenuEntity.getName(),
                        sysMenuEntity.getStatus(),
                        sysMenuEntity.getUrl(),
                        sysMenuEntity.getLevel(),
                        sysMenuEntity.getSort());
        return sysMenuDto;
    }

    public static TreeSet<ShowInsertUpdateSysMenuDto> createShowInsertUpdateSysMenuDtoSet(TreeSet<SysMenuEntity> menuSet) {
        TreeSet<ShowInsertUpdateSysMenuDto> sysMenuDtoSet =
                new TreeSet<ShowInsertUpdateSysMenuDto>(
                        new Comparator<ShowInsertUpdateSysMenuDto>() {
                            @Override
                            public int compare(ShowInsertUpdateSysMenuDto o1, ShowInsertUpdateSysMenuDto o2) {
                                if (o1.getId().equals(o2.getId())) {
                                    return 0;
                                }
                                if (o1.getSort() - o2.getSort() == 0) {
                                    return 1;
                                }
                                return o1.getSort() - o2.getSort();
                            }
                        });
        if (menuSet != null && menuSet.size() > 0) {
            Iterator<SysMenuEntity> sysMenuEntityIterator = menuSet.iterator();
            while (sysMenuEntityIterator.hasNext()) {
                SysMenuEntity sysMenuEntity = sysMenuEntityIterator.next();
                sysMenuDtoSet.add(ShowInsertUpdateSysMenuDto.createSysMenuDto(sysMenuEntity));
            }
        }
        return sysMenuDtoSet;
    }
}
